package com.warm.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.warm.core.domain.AjaxResult;
import com.warm.core.domain.R;
import com.warm.core.page.BaseController;
import com.warm.core.page.TableDataInfo;
import com.warm.core.utils.file.FileUploadUtils;
import com.warm.excel.poi.utils.ExcelUtil;
import com.warm.log.annotation.Log;
import com.warm.log.enums.BusinessType;
import com.warm.orm.domain.sys.dto.SysHotPlugDto;
import com.warm.orm.domain.sys.vo.SysHotPlugVo;
import com.warm.system.service.ISysHotPlugService;
import io.swagger.annotations.ApiOperation;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.UploadedFile;
import org.noear.solon.validation.annotation.Valid;
import org.noear.solon.validation.annotation.Validated;

import java.io.IOException;
import java.util.List;

/**
 * 扩展插件控制器
 *
 * @author xian.zi.ming@qq.com
 */
@Valid
@Controller
@Mapping("/system/HotPlug")
public class SysHotPlugController extends BaseController {
    @Inject
    private ISysHotPlugService sysHotPlugService;

    /**
     * 分页查询扩展插件列表
     */
    @ApiOperation("分页查询扩展插件列表")
    @SaCheckPermission("system:hot-plug:list")
    @Get
    @Mapping("/list")
    public TableDataInfo<SysHotPlugVo> list(SysHotPlugDto dto) {
        return sysHotPlugService.selectPage(dto);
    }


    @Post
    @Mapping("/upload")
    public String upload(UploadedFile file) throws IOException { // 表单变量名要跟参数名对上
        return sysHotPlugService.uploadPlug(file); // 把它转为本地文件
    }


    /**
     * 导出扩展插件列表
     */
    @ApiOperation("导出扩展插件列表")
    @Log(title = "扩展插件", businessType = BusinessType.EXPORT)
    @SaCheckPermission("system:dict:export")
    @Post
    @Mapping("/export")
    public void export(Context ctx, SysHotPlugDto dictType) {
        List<SysHotPlugVo> list = sysHotPlugService.selectList(dictType);
        ExcelUtil<SysHotPlugVo> util = new ExcelUtil<SysHotPlugVo>(SysHotPlugVo.class);
        util.exportExcel(ctx, list, "字典类型");
    }

    /**
     * 获取扩展插件详细信息
     */
    @ApiOperation("获取扩展插件详细信息")
    @SaCheckPermission("system:hot-plug:query")
    @Get
    @Mapping(value = "/{qualifiedName}")
    public R<SysHotPlugVo> getInfo(String qualifiedName) {
        return R.ok(sysHotPlugService.selectById(qualifiedName));
    }

    /**
     * 新增扩展插件
     */
    @SaCheckPermission("system:dict:add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @Post
    @Mapping
    public AjaxResult add(@Validated SysHotPlugDto dto) {
        if (!sysHotPlugService.checkDictTypeUnique(dto)) {
            return error("新增插件'" + dto.getQualifiedName() + "'失败，字典类型已存在");
        }
        return toAjax(sysHotPlugService.insertByDto(dto));
    }

    /**
     * 修改扩展插件
     */
    @ApiOperation("修改扩展插件")
    @SaCheckPermission("system:hot-plug:edit")
    @Log(title = "扩展插件", businessType = BusinessType.UPDATE)
    @Put
    @Mapping
    public R<Void> edit(SysHotPlugDto dto) {
        return toR(sysHotPlugService.updateByDto(dto));
    }

    /**
     * 删除扩展插件
     */
    @ApiOperation("删除扩展插件")
    @SaCheckPermission("system:hot-plug:remove")
    @Log(title = "扩展插件", businessType = BusinessType.DELETE)
    @Delete
    @Mapping("/{dictIds}")
    public AjaxResult remove(String[] qualifiedNames) {
        sysHotPlugService.deleteByIds(qualifiedNames);
        return success();
    }
}
